package cn.bocweb.spinning.view.forun;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.SocietyDetailBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import de.greenrobot.event.EventBus;

public class ForunSocialActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.forun_social_activity_list)
    ListView forunSocialActivityList;
    @BindColor(R.color.white)
    int whith;
    boolean isLastPage;
    ForumSocialAdapter mForumSocialAdapter;
    int page = 1;
    String id, userid;
    SwipeRefreshHelper mSwipeRefreshHelper;

    UserPresenter mPresenter;
    List<SocietyDetailBean.DataBean> list;
    @Bind(R.id.refresh)
    SwipeRefreshLayout mRefresh;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.today)
    TextView mToday;
    @Bind(R.id.history)
    TextView mHistory;
    @Bind(R.id.member)
    TextView mMember;
    @Bind(R.id.title_icon)
    RelativeLayout mTitleIcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forun_social);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);

        list = new ArrayList<>();
        mForumSocialAdapter = new ForumSocialAdapter(this, list);
        mPresenter = new UserPresenterImp();

        initToolbar();
        initListView();
        initIntent();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStop() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onStop();
    }

    public void onEventMainThread(SocietyDetailBean bean) {
        if (mRefresh.isRefreshing()) {
            mRefresh.setRefreshing(false);
        }
        if (bean.getReturn_code() == 0) {
            String today = bean.getToday() == null ? "0" : bean.getToday();
            String history = bean.getTotal() == null ? "0" : bean.getTotal();
            String member = bean.getPeople() == null ? "0" : bean.getPeople();
            mToday.setText(today);
            mHistory.setText(history);
            mMember.setText(member);

            list.addAll(bean.getData());
            mForumSocialAdapter.notifyDataSetChanged();
        } else if (bean.getReturn_code() == -202){
            isLastPage = true;
            DeviceUtil.MyToast(this, "已经是最后一页");
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    private void initIntent() {
        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        userid = dataEntity.getUserid();

        Intent intent = getIntent();
        int position = intent.getIntExtra(Constant.TO_FORUM_SOCIETY_COLOR, 0);
        id = intent.getStringExtra(Constant.TO_FORUM_SOCIETY);
        String title = intent.getStringExtra(Constant.TO_FORUM_SOCIETY_TITLE);
        mPresenter.societyDetail(page, userid, id);

        initTitleIcon(position, title);
    }

    private void initTitleIcon(int position, String title) {
        List<Integer> colorList = initColorList();
        int colorPosition = position % colorList.size();
        int color = colorList.get(colorPosition);
        mTitleIcon.setBackgroundResource(color);
        mTitle.setText(title);
    }

    private List<Integer> initColorList() {
        List<Integer> colorList = new ArrayList<>();
        colorList.add(R.drawable.society_1);
        colorList.add(R.drawable.society_2);
        colorList.add(R.drawable.society_3);
        colorList.add(R.drawable.society_4);
        colorList.add(R.drawable.society_5);
        colorList.add(R.drawable.society_6);
        colorList.add(R.drawable.society_7);
        colorList.add(R.drawable.society_8);
        colorList.add(R.drawable.society_9);
        colorList.add(R.drawable.society_10);
        colorList.add(R.drawable.society_11);
        colorList.add(R.drawable.society_12);
        colorList.add(R.drawable.society_13);
        colorList.add(R.drawable.society_14);
        colorList.add(R.drawable.society_15);
        return colorList;
    }

    private void initListView() {
        forunSocialActivityList.setAdapter(mForumSocialAdapter);

        mSwipeRefreshHelper = new SwipeRefreshHelper(mRefresh, forunSocialActivityList, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                list.clear();
                isLastPage = false;
                mForumSocialAdapter.notifyDataSetChanged();
                mPresenter.societyDetail(page, userid, id);
            }

            @Override
            public void onLoad() {
                if (isLastPage) {
//                    DeviceUtil.MyToast(ForunSocialActivity.this, "已经是最后一页");
                } else {
                    page ++;
                    mPresenter.societyDetail(page, userid, id);
                }
            }
        });

        forunSocialActivityList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ForunSocialActivity.this, ForunDetailActivity.class);
                intent.putExtra(Constant.TO_FORUM_DETAIL, list.get(position).getId());
                startActivity(intent);
            }
        });
    }

    private void initToolbar() {
        toolbar.setTitle("社区");
        toolbar.setTitleTextColor(whith);
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
